package tms.controler;

import java.util.Date;

import tms.Cache;
import tms.model.Account;
import tms.service.ServiceHandler;
import tms.view.Tools;

public class AccountController {

    /***** New function here !! *************************************************/

    /**
     * Create new Account with all information.
     * @param firstName
     * @param lastName
     * @param email
     * @param personalID
     * @param address
     * @param phone
     * @param frequentFlyNumber use Tools.newFrequentFlightNumber() to create a new one
     * @param flyScore
     * @param registrationDate
     * @param password
     * @param username
     * @param city
     * @param province
     * @param country
     * @param passportNumber
     * @param birthDay
     * @param issuingCountry
     * @return account created
     */
    private Account createAccountFullInfo(String title, String firstName,
            String lastName, String email, String personalID, String address,
            String phone, String password, String username, String city,
            String province, String country, String passportNumber, Date birthDay,
            String issuingCountry) {

        Account acc = new Account();
        acc.setTitle(title);
        acc.setFirstName(firstName);
        acc.setLastName(lastName);
        acc.setEmail(email);
        acc.setPidNumber(personalID);
        acc.setAddress(address);
        acc.setPhone(phone);
        acc.setFfNumber(Tools.generateString(Tools.FLIGHT_NUMBER_PREFIX));
        acc.setFfScore(0);
        acc.setRegistrationDate(new Date());
        acc.setPassword(password);
        acc.setUsername(username);
        acc.setCity(city);
        acc.setProvince(province);
        acc.setCountry(country);
        acc.setPassportNumber(passportNumber);
        acc.setBirthDate(birthDay);
        acc.setIssuingCountry(issuingCountry);
        acc.setActive(true);
        acc.setAccountNumber(Tools.generateString(Tools.ACCOUNT_NUMBER_PREFIX));

        return acc;
    }

    /**
     * Create new user, if success user is store in Cache.CURRENT_USER.
     * @param current
     * @param title
     * @param firstName
     * @param lastName
     * @param email
     * @param personalID
     * @param address
     * @param phone
     * @param password
     * @param username
     * @param city
     * @param province
     * @param country
     * @param passportNumber
     * @param birthDay
     * @param issuingCountry
     * @return true if success otherwise false
     */
    public boolean createOrUpdate(Account current, String title, String firstName,
            String lastName, String email, String personalID, String address,
            String phone, String password, String username, String city,
            String province, String country, String passportNumber, Date birthDay,
            String issuingCountry) {

        //Update information
        if (current != null) {
            current.setTitle(title);
            current.setFirstName(firstName);
            current.setLastName(lastName);
            current.setEmail(email);
            current.setPidNumber(personalID);
            current.setAddress(address);
            current.setPhone(phone);
            current.setPassword(password);
            current.setUsername(username);
            current.setCity(city);
            current.setProvince(province);
            current.setCountry(country);
            current.setPassportNumber(passportNumber);
            current.setBirthDate(birthDay);
            current.setIssuingCountry(issuingCountry);
        } else {
            //create new user
            current = createAccountFullInfo(title, firstName, lastName, email, personalID, address,
                    phone, password, username, city, province, country, passportNumber,
                    birthDay, issuingCountry);
        }

        if (ServiceHandler.saveAccount(current)) {
            Cache.currentUser = current;
            return true;
        } else {
            return false;
        }
    }




    /****************************************************************************/


}
